package cn.felord.debezium.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
@Slf4j
public class String2AcsII16 {

    public static int hext2Int(String hexString){
       return Integer.parseInt(hexString, 16);
    }
    public static String hext2Int(Integer asciiValue10){
            String hexString = Integer.toHexString(asciiValue10);
            return hexString;
    }
    public static String go(String str){
        StringBuffer buffer = new StringBuffer();
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int asciiValue = chars[i];
//            System.out.println(chars[i] + " 的 ASCII 值为：" + asciiValue);
            String s = Integer.toHexString((int) chars[i]);
//            System.out.println(chars[i] + " 16进制 值为：" + s);
            int cut = 4 - s.length();
            if(s.length() < 4){
                for(int j = 0;j < cut ;j++){
                    s = "0" + s;
                }
            }
            buffer.append(s);
//            System.out.println(chars[i] + " 四位的 16进制 值为：" + s);
        }
        return buffer.toString();
    }

    /**
     * String转 10进制 ACSII 只转一位
     * @param str
     * @return
     */
    public static int string2Char2asciiValue(String str){
        //  String str = "C";
//        if(str.length() > 1){
//            char c = str.charAt(0);
//            char c1 = str.charAt(1);
//        }
        char c = str.charAt(0);
//        System.out.println(c);
        int asciiValue = c;  //char 转 int
        return asciiValue;
    }

    /**
     * 字符串转 16 两位（两字节）进制的字符串 占位00
     * @param str
     * @return
     */
    public static String string2asciiValue16_2(String str){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int asciiValue = c;  //char 转 int
            String s = int10to16_2(asciiValue);
            sb.append(s);
        }
        return sb.toString();
    }
    public static String string2Char2asciiValueint10to16(String str){
        return int10to16_4(string2Char2asciiValue(str));
    }
    public static String string2Char2asciiValueint10to16_2(String str){
        return int10to16_2(string2Char2asciiValue(str));
    }

    /**
     * 10进制ACSII转四位 16进制ACSII
     * Integer.toHexString(asciiValue10)  10 进制 ACSII
     * @param asciiValue10
     * @return
     */
    public static String int10to16_4(int asciiValue10){
        if(asciiValue10 > 9999){
            log.error("10进制转16进制数据超过四位数，请核对标准或其他数据！！！！！！！！！！！！！");
            asciiValue10 = 9999;
        }
        String s16 = Integer.toHexString(asciiValue10);
        int cut = 4 - s16.length();//补00
        if(s16.length() < 4){
            for(int j = 0;j < cut ;j++){
                s16 = "0" + s16;
            }
        }
        return s16;
    }
    /**
     * 10进制ACSII转 两位 16进制ACSII
     * @param asciiValue10
     * @return
     */
    public static String int10to16_2(int asciiValue10){
        String s16 = Integer.toHexString(asciiValue10);
        int cut = 2 - s16.length();
        if(s16.length() < 2){
            for(int j = 0;j < cut ;j++){
                s16 = "0" + s16;
            }
        }
//        System.out.println( " 二位的 16进制 值为：" + s16);
        return s16;
    }

    public static String hexToString(String hex) {
        return new String(new BigInteger(hex, 16).toByteArray());
    }

    public static void main(String[] args) {
        String s16 = Integer.toHexString(24);
        System.out.println(s16);
        System.out.println(hexToString("4B44"));
        System.out.println(hexToString("FE01100200001c00384B445443303030330000000000000041000000000000004200000000000000430000000000000044000000000000004500000000000000007032FF"));
    }

}
